package com.fairytask.engine.components.group
{
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.BaseItem;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	/**
	 * 显示列表组，用于将特定的显示对象以组的方式呈现 
	 * @author Hanjy
	 * 
	 */	
	public class ViewGroup extends BaseComponent
	{
		private var _baseItemRes	:String;
		
		private var _pageTotal		:uint;
		
		private var _pageNow		:int;
		
		private var _items			:Array;
		
		private var _maxRow		:uint;
		
		private var _maxCol		:uint;
		
		private var _itemsWidth	:Number		=	0;
		
		private var _itemsHeight	:Number		=	0;
		
		private var _offsetX		:int		=	0;
		
		private var _offsetY		:int		=	0;
		
		private var _offsetRow		:int		=	2;
		
		private var _offsetCol		:int		=	2;
		
		private var _bg				:Sprite		=	new Sprite();
		
		public function ViewGroup(row:uint,col:uint,baseItemRes:String,pw:int=0,ph:int=0,backSkin:String="")
		{
			super();			
			_maxRow			=	row;
			_maxCol			=	col;
			_baseItemRes	=	baseItemRes;
			
			_items			=	new Array();
			_pageTotal		=	1;
			_pageNow		=	-1;
			_bg				=	ResManager.newResClass(backSkin) as Sprite;
			
			for(var i:uint = 0 ; i < _pageTotal ; i ++)
			{
				_items[i]	=	new Array();
				for(var j:uint = 0 ; j < _maxRow ; j ++)
				{
					_items[i][j]	=	new Array();
					for(var k:uint = 0 ; k < _maxCol ; k ++)
					{
						_items[i][j][k]			=	new BaseItem(_baseItemRes,pw,ph);//ResManager.newResClass();
					}
				}
			}
			
			var tClip:DisplayObject	=	ResManager.newResClass(_baseItemRes);
			
			if(pw==0)
			{
				_itemsWidth		=	tClip.width;
				_itemsHeight	=	tClip.height;
			}
			else
			{
				_itemsWidth		=	pw;
				_itemsHeight	=	ph;
				tClip.width		=	pw;
				tClip.height	=	ph;
			}
			
			reSizeItems();
			
			page			=	0;
		}
		private function reSizeItems():void
		{
			for(var i:uint = 0 ; i < _pageTotal ; i ++)
			{
				for(var j:uint = 0 ; j < _maxRow ; j ++)
				{
					for(var k:uint = 0 ; k < _maxCol ; k ++)
					{
						_items[i][j][k].x	=	k*(_itemsWidth	+_offsetCol)+_offsetX;
						_items[i][j][k].y	=	j*(_itemsHeight	+_offsetRow)+_offsetY;
					}
				}
			}
		}
		
		public function addPage():void
		{
			var tArr:Array	=	new Array();
			
			for(var i:uint = 0 ; i < _maxRow ; i ++)
			{
				tArr[i]	=	new Array();
				for(var j:uint = 0 ; j < _maxCol ; j ++)
				{
					tArr[i][j]	=	new BaseItem(_baseItemRes,_itemsWidth,_itemsHeight);
				}
			}
			
			_items.push(tArr);
			
			_pageTotal	=	_items.length;
			reSizeItems();
		}
		
		public function getItemByIndex(index:int):BaseItem
		{
			var cPage			:uint	=	int(index/(_maxCol*_maxRow));	//当前页数??
			if(cPage>_items.length-1)	//???
			{
				addPage();
			}
			var temp			:uint	=	index - cPage*_maxCol*_maxRow;	//
			var cRow			:uint	=	int(temp/_maxCol);
			var cCol			:uint	=	temp-cRow*_maxCol;
		
			return _items[cPage][cRow][cCol];
		}
		
		public function getItemByPageIndex(index:uint):BaseItem
		{
			var cRow			:uint	=	int(index/_maxCol);
			var cCol			:uint	=	index-cRow*_maxCol;
			
			return _items[_pageNow][cRow][cCol];
		}
		public function removePage():void
		{
			if(_pageNow == _pageTotal-1)
			{
				page	=	page-1;
			}			
			_items.pop();
			_pageTotal	=	_items.length;
		}
		public function getItem(pageVal:uint,row:uint,col:uint):BaseItem
		{
			return _items[pageVal][row][col]
		}
		
		public function getItemForEach(func:Function):void
		{
			var row	:uint	=	_items.length;
			var col	:uint	=	0;
			
			if(row>0)
			{
				col			=	_items[0].length;
			}
			
			for(var i:uint = 0 ; i < _pageTotal ; i ++)
			{
				for(var j:uint = 0 ; j < _maxRow ; j ++)
				{
					for(var k:uint = 0 ; k < _maxCol ; k ++)
					{
						func.call(this,_items[i][j][k]);
					}
				}
			}
			
		}
		
		public function addItemsEvent(type:String,listener:Function):void
		{
			for(var i:uint	=	0 ; i < pageTotal ; i++)
			{
				for(var j:uint	=	0 ; j <_maxRow ; j++)
				{
					for(var k:uint = 0 ; k < _maxCol ; k ++)
					{
						if(type==MouseEvent.DOUBLE_CLICK)
						{
							_items[i][j][k].doubleClickEnabled	=	true;
							_items[i][j][k].mouseChildren		=	false;
						}
						_items[i][j][k].addItemEvent(type,listener,i*_maxRow+j*_maxCol+k);
					}					
				}
			}
		}
		
		public function removeItemEvent(type:String,listener:Function):void
		{
			var row	:uint	=	_items.length;
			var col	:uint	=	0;
			
			if(row>0)
			{
				col			=	_items[0].length;
			}
			
			for(var i:uint	=	0 ; i < pageTotal ; i++)
			{
				for(var j:uint	=	0 ; j < _maxRow ; j++)
				{
					for(var k:uint = 0 ; k < _maxCol ; k ++)
					{
						_items[i][j][k].removeItemEvent(type,listener);
					}
				}
			}
		}
		
		public function clearEvent():void
		{
			var row	:uint	=	_items.length;
			var col	:uint	=	0;
			
			if(row>0)
			{
				col			=	_items[0].length;
			}
			
			for(var i:uint	=	0 ; i < pageTotal ; i++)
			{
				for(var j:uint	=	0 ; j < _maxRow ; j++)
				{
					for(var k:uint = 0 ; k < _maxCol ; k ++)
					{
						_items[i][j][k].clearEvent();
					}
				}
			}
		}
		
		public function reSet():void
		{
			clearEvent();
			
			_items			=	new Array();
			_pageTotal		=	1;
			_pageNow		=	-1;			
			
			for(var i:uint = 0 ; i < _pageTotal ; i ++)
			{
				_items[i]	=	new Array();
				for(var j:uint = 0 ; j < _maxRow ; j ++)
				{
					_items[i][j]	=	new Array();
					for(var k:uint = 0 ; k < _maxCol ; k ++)
					{
						_items[i][j][k]	=	new BaseItem(_baseItemRes,_itemsWidth,_itemsHeight);
					}
				}
			}
			
			var tClip:DisplayObject	=	ResManager.newResClass(_baseItemRes);
			
			if(_itemsWidth==0)
			{
				_itemsWidth		=	tClip.width;
				_itemsHeight	=	tClip.height;
			}
			else
			{
				tClip.width		=	_itemsWidth;
				tClip.height	=	_itemsHeight;
			}
			
			reSizeItems()
			
			page			=	0;
		}
		
		public function clearData():void
		{
			while(pageTotal>1)
			{
				removePage();	
			}
		}

		public function get offsetX():int
		{
			return _offsetX;
		}

		public function set offsetX(value:int):void
		{
			_offsetX = value;
			reSizeItems();
		}

		public function get offsetY():int
		{
			return _offsetY;
		}

		public function set offsetY(value:int):void
		{
			_offsetY = value;
			reSizeItems();
		}

		/**行间距*/
		public function get offsetRow():int
		{
			return _offsetRow;
		}

		/**
		 * @private
		 */
		public function set offsetRow(value:int):void
		{
			_offsetRow = value;
			reSizeItems();
		}

		/**列间距*/
		public function get offsetCol():int
		{
			return _offsetCol;
		}

		/**
		 * @private
		 */
		public function set offsetCol(value:int):void
		{
			_offsetCol = value;
			reSizeItems();
		}

		public function get page():uint
		{
			return _pageNow;
		}

		public function set page(value:uint):void
		{
			if(value<0||value>_items.length-1)
			{
				return;
			}
			if(_pageNow != value)
			{
				var i:uint = 0 ;
				var j:uint = 0 ;
				
				if(_pageNow >-1)
				{
					for(i = 0 ; i < _maxRow ; i ++)
					{
						for(j = 0 ; j < _maxCol ; j ++)
						{
							if(_items[_pageNow][i][j].parent != null)
							{
								removeChild(_items[_pageNow][i][j]);
							}
						}
					}
				}
				
				for(i = 0 ; i < _maxRow ; i ++)
				{
					for(j = 0 ; j < _maxCol ; j ++)
					{
						
						addChild(_items[value][i][j]);
					}
				}
			}
			_pageNow = value;
		}
		/**
		 *	选中状态 
		 * 
		 */		
		public function selectItem(index:int):void
		{
			var baseItem	:BaseItem	=	getItemByIndex(index);
			
			
		}

		public function get pageTotal():uint
		{
			return _items.length;
		}
		
		public function get maxRow():uint
		{
			return _maxRow;
		}
		
		public function get maxCol():uint
		{
			return _maxCol;
		}

		public function get itemsWidth():Number
		{
			return _itemsWidth;
		}

		public function set itemsWidth(value:Number):void
		{
			_itemsWidth = value;
			reSizeItems();
		}

		public function get itemsHeight():Number
		{
			return _itemsHeight;
		}

		public function set itemsHeight(value:Number):void
		{
			_itemsHeight = value;
			reSizeItems();
		}
	}
}